﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.IO;
using PaperSystem.Models;
using Webdiyer.WebControls.Mvc;//引入分页类库

namespace PaperSystem.Controllers
{
    public class PaperController : Controller
    {
        //
        // GET: /Paper/
        PaperRepository _paperRepository;
        PublicRepository _publicRepository;
        public PaperController()
        {
            _paperRepository = new PaperRepository();
            _publicRepository = new PublicRepository();
        }

        //======================================================教师使用======================================================
        //显示试卷基本信息
        public ActionResult PaperList(PaperQuery info,int? page)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            List<PaperList> paperList = _paperRepository.GetPaperList(info);
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            //用于分页
            PagedList<PaperList> plist = new PagedList<PaperList>(paperList, page ?? 1, 30);
            return View("PaperList", plist);
        }

        //新增试卷（显示）
        public ActionResult PaperAdd(QuestionQuery info)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //TODO：还要适当调整info内容，再根据info确定要哪些信息
            List<QuestionList> questionList = _publicRepository.GetQuestionList(info);//新增试卷获取所有题目外表信息
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            ViewData["type"] = _publicRepository.GetTypeDropdownList();
            TempData["begin_message"] = "";
            return View(questionList);
        }

        //新增试卷（提交）
        [HttpPost]
        public ActionResult PaperAdd(NewPaper paper,PaperObject[] paperObject,PaperInfo[] paperInfo)
        {
            string userId = "";
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie

            paper.creatorId = userId;
            paper.createTime = DateTime.Now;

            if (_paperRepository.AddPaper(paper, paperObject, paperInfo))
            {
                TempData["begin_message"] = "添加成功";
            }
            else
            {
                TempData["begin_message"] = "添加失败";
            }
            //TODO：还要适当调整info内容，再根据info确定要哪些信息
            List<QuestionList> questionList = _publicRepository.GetQuestionList(null);//新增试卷获取所有题目外表信息
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            ViewData["type"] = _publicRepository.GetTypeDropdownList();
            return View(questionList);
        }

        //不刷新查询题目信息
        [HttpPost]
        public JsonResult PaperAddQueryQuestion(string course, string type, string TorE, string chapter, string knowledge, string creator)
        {
            List<QuestionList> questionList = _publicRepository.GetQuestionList(course, type, TorE, chapter, knowledge, creator);
            return Json(questionList, JsonRequestBehavior.AllowGet);
        }

        //新增试卷获取题目具体内容
        [HttpPost]
        public JsonResult PaperAddQuestion(int eid)
        {
            UpdateQuestion question = _paperRepository.GetUpdateQuestion(eid);
            return Json(question,JsonRequestBehavior.AllowGet);//System.Web.Helpers.Json.Encode()//Json方法不知可否转换复杂的类对象，只知道可以转简单的类对象
        }

        //删除试卷（提交）
        [HttpPost]
        public ActionResult PaperDelete(string pid, PaperQuery info)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            if (_paperRepository.DeletePaper(pid))
            {
                TempData["begin_message"] = "删除成功";
            }
            else
            {
                TempData["begin_message"] = "删除失败";
            }
            //return PaperList(info,null);
            return RedirectToAction("PaperList", "Paper", info);
        }

        //修改试卷（显示）
        public ActionResult PaperUpdate(string pid)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //验证pid是否合法
            int id = -1;
            if (pid == null || pid == "" || !int.TryParse(pid, out id))
            {
                return RedirectToAction("PaperList");
            }//end 验证pid是否合法
            UpdatePaper paper = _paperRepository.GetUpdatePaper(id);
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            ViewData["type"] = _publicRepository.GetTypeDropdownList();
            TempData["begin_message"] = "";
            if (_paperRepository.IsPaperUsed(id))
            {
                TempData["hasUsed"] = "hasUsed";
            }
            else
            {
                TempData["hasUsed"] = "";
            }
            return View(paper);
        }

        //修改试卷（提交）
        [HttpPost]
        public ActionResult PaperUpdate(UpdatePaperPost paper, UpdatePaperObjectPost[] paperObject, UpdatePaperInfo[] paperInfo)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            if(_paperRepository.UpdatePaper(paper, paperObject, paperInfo))
            {
                TempData["begin_message"] = "修改成功";
            }
            else
            {
                TempData["begin_message"] = "修改失败";
            }
            UpdatePaper newpaper = _paperRepository.GetUpdatePaper(paper.pid);
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            ViewData["type"] = _publicRepository.GetTypeDropdownList();
            return View(newpaper);
        }

        //下载试卷word文档
        [HttpPost]
        public ActionResult PaperDownload(int pid)
        {
            FileStream fileStream = _paperRepository.DownloadPaper(pid);
            return File(fileStream, "text/plain", "未命名.doc");
        }

        //改卷（显示所有考试试卷列表）
        public ActionResult PaperCorrectList1(PaperQuery info, int? page)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            info.TorE = "考试";
            List<PaperList> paperList = _paperRepository.GetPaperList(info);
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            //用于分页
            PagedList<PaperList> plist = new PagedList<PaperList>(paperList, page ?? 1, 30);
            return View("PaperCorrectList1", plist);
        }

        //改卷（显示该张试卷做过的用户信息列表）
        public ActionResult PaperCorrectList2(ExamUserInfo info, int? page)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //验证pid是否合法
            int id = -1;
            if (info.pid == null || info.pid == "" || !int.TryParse(info.pid, out id))
            {
                return RedirectToAction("PaperCorrectList1");
            }//end 验证pid是否合法
            List<ExamUserInfo> examUserInfo = _paperRepository.GetExamUser(info);
            Paper paper = _publicRepository.GetPaperInfo(id);
            ViewData["paperInfo"] = paper;
            ViewData["paperId"] = paper.Pid;
            ViewData["Corrected"] = _publicRepository.GetCorrectedDropdownList();
            //用于分页
            PagedList<ExamUserInfo> plist = new PagedList<ExamUserInfo>(examUserInfo, page ?? 1, 30);
            return View(plist);
        }

        //改卷（显示选中准备批改的试卷）
        public ActionResult PaperCorrect(AnswerQuery answerQuery)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            AnswerCompare examCompare = new AnswerCompare();
            if (answerQuery != null)
            {
                examCompare = _paperRepository.CompareAnswer(answerQuery,"考试");
            }
            return View(examCompare);
        }

        //改卷（提交所批改试卷的信息）
        [HttpPost]
        public ActionResult PaperCorrect(ExPaperAnswer answer)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie

            _paperRepository.FillScore(answer);//TODO可能失败

            //返回到List2，继续查看未批改的用户
            ExamUserInfo info = new ExamUserInfo();
            info.pid = answer.pid.ToString();
            return RedirectToAction("PaperCorrectList2", "Paper", info);
        }

        //======================================================学生使用======================================================
        //显示考试试卷基本信息
        public ActionResult ExamPaperList(PaperQuery info, int? page)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            info.TorE = "考试";
            List<PaperList> paperList = _paperRepository.GetPaperList(info);
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            //用于分页
            PagedList<PaperList> plist = new PagedList<PaperList>(paperList, page ?? 1, 20);
            return View("ExamPaperList", plist);
        }

        //考试（显示）
        public ActionResult ExamPaperUpdate(string pid)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //验证pid是否合法
            int id = -1;
            if (pid == null || pid == "" || !int.TryParse(pid, out id))
            {
                return RedirectToAction("ExamPaperList");
            }//end 验证pid是否合法
            PaperUsed paper = _paperRepository.GetPaperUsed(id);
            ViewData["isBegin"] = "false";
            return View(paper);
        }

        //考试（开始考试）
        public ActionResult ExamTimeBegin(string pid)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //验证pid是否合法
            int id = -1;
            if (pid == null || pid == "" || !int.TryParse(pid, out id))
            {
                return RedirectToAction("ExamPaperList");
            }//end 验证pid是否合法
            PaperUsed paper = _paperRepository.GetPaperUsed(id);

            //设置考试开始时间cookie
            HttpCookie currentCookie = Request.Cookies["beginTime"];
            //如果原来没有，就要重新设置
            if (currentCookie != null)
            {
                DateTime time = new DateTime();
                if (DateTime.TryParse(currentCookie.Value.ToString(), out time))
                {
                    paper.beginTime = time;
                }
                else
                {
                    paper.beginTime = DateTime.Now;
                    currentCookie = new HttpCookie("beginTime", paper.beginTime.ToString());
                    currentCookie.Expires = DateTime.Now.AddMinutes((double)paper.usedMinute);
                    this.Response.Cookies.Add(currentCookie);
                }
            }
            else
            {
                paper.beginTime = DateTime.Now;
                currentCookie = new HttpCookie("beginTime", paper.beginTime.ToString());
                currentCookie.Expires = DateTime.Now.AddMinutes((double)paper.usedMinute);
                this.Response.Cookies.Add(currentCookie);
            }
            ViewData["isBegin"] = "true";
            return View("ExamPaperUpdate", paper);
        }

        //考试（提交）
        [HttpPost]
        public ActionResult ExamPaperUpdate(ExPaperAnswer answer)
        {
            string userId = "";
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //删除名为“beginTime”的cookie
            HttpCookie currentCookie = new HttpCookie("beginTime", "");
            currentCookie.Expires = DateTime.Now.AddMinutes(-1);
            this.Response.Cookies.Add(currentCookie);

            answer.userId = userId;
            _paperRepository.PostPaperAnswer(answer);
            return RedirectToAction("ExamPaperList", "Paper");
        }

        //显示练习试卷基本信息
        public ActionResult ExercisePaperList(PaperQuery info, int? page)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            info.TorE = "练习";
            List<PaperList> paperList = _paperRepository.GetPaperList(info);
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            //用于分页
            PagedList<PaperList> plist = new PagedList<PaperList>(paperList, page ?? 1, 20);
            return View("ExercisePaperList", plist);
        }

        //练习（显示）
        public ActionResult ExercisePaperUpdate(string pid)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //验证pid是否合法
            int id = -1;
            if (pid == null || pid == "" || !int.TryParse(pid, out id))
            {
                return RedirectToAction("ExercisePaperList");
            }//end 验证pid是否合法
            PaperUsed paper = _paperRepository.GetPaperUsed(id);
            ViewData["isBegin"] = "false";
            return View(paper);
        }

        //练习（开始练习）
        public ActionResult ExerciseTimeBegin(string pid)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            //验证pid是否合法
            int id = -1;
            if (pid == null || pid == "" || !int.TryParse(pid, out id))
            {
                return RedirectToAction("ExercisePaperList");
            }//end 验证pid是否合法
            PaperUsed paper = _paperRepository.GetPaperUsed(id);

            paper.beginTime = DateTime.Now;
            ViewData["isBegin"] = "true";
            return View("ExercisePaperUpdate", paper);
        }

        //练习（提交）
        [HttpPost]
        public ActionResult ExercisePaperUpdate(ExPaperAnswer answer)
        {
            string userId = "";
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            answer.userId = userId;
            _paperRepository.PostPaperAnswer(answer);//TODO可能失败？

            AnswerQuery answerQuery = new AnswerQuery();
            //answerQuery.beginTime = DateTime.Now;
            answerQuery.beginTime = answer.beginTime;
            answerQuery.pid = answer.pid;
            answerQuery.userId = userId;

            return RedirectToAction("ExercisePaperCompare", "Paper", answerQuery);
        }

        //练习提交后显示已填答案和参考答案
        public ActionResult ExercisePaperCompare(AnswerQuery answerQuery)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "学生")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            AnswerCompare exerciseCompare = new AnswerCompare();
            if (answerQuery != null)
            {
                exerciseCompare = _paperRepository.CompareAnswer(answerQuery,"练习");
            }
            return View(exerciseCompare);
        }
    }
}
